package com.hkemmm.shop.controller;

import com.hkemmm.shop.entity.Car;
import com.hkemmm.shop.entity.Goods;
import com.hkemmm.shop.entity.User;
import com.hkemmm.shop.service.ICarService;
import com.hkemmm.shop.service.IGoodsService;
import com.hkemmm.shop.service.IOrderService;
import com.hkemmm.shop.service.UserService;
import com.hkemmm.shop.utlis.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.converter.json.JsonbHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
@RequestMapping("/shop")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ICarService carService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IGoodsService goodsService;

    @RequestMapping("/getUser")
    @ResponseBody
    public String getUser(Integer id) {
        User user = userService.selectByPrimaryKey(id);
        return user.toString();
    }

    @ResponseBody
    @RequestMapping("/test")
    public String test(Integer x, Character y, String z) {
        System.out.println(x + "-" + y + "-" + z);


        return "ok" + x + "-" + y + "-" + z;
    }


    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping("/login")
    public Object selectUser(String username, String password, HttpServletRequest request, HttpServletResponse response) {

        List<Goods> goods = goodsService.getGoods(5);
        request.getSession().setAttribute("random", goods);

        User user = userService.selectUser(username, password);
        if (user == null) {
            return null;
        } else {
            request.getSession().setAttribute("User", user);
            return user;
        }

    }

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("/insert")
    public String insertUser(User user) {
        user.setCredit(0);
        user.setStatus(0);
        user.setPhoto("head1.jpg");


        System.out.println(user);
        try {
            userService.insertSelective(user);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "ok";
    }


    @ResponseBody
    @RequestMapping("/exist")
    public String existUser(String username) {
        User user = userService.existUser(username);

        if (user == null) {
            return "no";
        }

        return "yes";
    }

    @RequestMapping("/exit")
    public String exit(HttpServletRequest request) {
        request.removeAttribute("User");

        return "/page/login";
    }

    @RequestMapping("/car")
    public ModelAndView goCart(Integer id, ModelAndView mav) {
        if (id == null) {
            mav.setViewName("/page/login");
        } else {
            List<Car> carList = carService.CarInfo(id);
            mav.addObject("carts", carList);
            mav.setViewName("/page/car");
        }

        return mav;
    }

    @RequestMapping("/centre")
    public ModelAndView goCentre(ModelAndView mav, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("User");
        if (user == null) {
            mav.setViewName("/page/login");
        } else {
            mav.addObject("payNum", orderService.countOrder(user.getId(), 0));
            mav.addObject("paidNum", orderService.countOrder(user.getId(), 1));

            mav.setViewName("/WEB-INF/pages/mygxin");
        }

        return mav;
    }

    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String updatePassword(User user) {
        userService.updateByPrimaryKeySelective(user);
        return "ok";
    }

    @ResponseBody
    @RequestMapping(value = "/updateInfo", method = RequestMethod.POST)
    public String updateInfo(User user, HttpServletRequest request) {
        userService.updateByPrimaryKeySelective(user);
        User u = userService.selectByPrimaryKey(user.getId());
        request.getSession().setAttribute("User", u);

        return "ok";
    }

    @RequestMapping(value = "/uploadPhoto", method = RequestMethod.POST)
    @ResponseBody
    public String uploadPhoto(MultipartFile file, Integer id, HttpServletRequest request) {

        String fileName = UploadFile.uploadFile(file, "E:\\home\\upload\\");

        int i = userService.updatePhoto(fileName, id);

        User u = userService.selectByPrimaryKey(id);
        request.getSession().setAttribute("User", u);

        if (i == 1) {
            return "ok";
        }
        return "err";
    }

}
